﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Resto.CostControl.Core.Domain.Query.MaterialModule;
using Enterprise.Framework.Core.Container;
using Resto.CostControl.Core.Facade.MaterialModule.DataTransferObject;
using Resto.CostControl.Core.Domain.Model;
using Resto.CostControl.Core.Enums;
using Enterprise.Framework.Frame.Architecture;

namespace Resto.CostControl.Core.Facade.MaterialModule.FacadeObject
{
    /// <summary>
    /// 原材料加工单FO
    /// </summary>
    public class MaterialProcessFO : IApplicationFacadeObject
    {
        #region 私有属性

        private MaterialProcessDQO _MaterialProcessDQO;
        private MaterialProcessDQO MaterialProcessDQO
        {
            get
            {
                if (_MaterialProcessDQO == null)
                    _MaterialProcessDQO = ObjectCreator.Create<MaterialProcessDQO>();

                return _MaterialProcessDQO;
            }
        }

        #endregion

        #region 公共方法

        /// <summary>
        /// 新增一个加工单
        /// </summary>
        /// <param name="processDTO"></param>
        /// <param name="processState">加工单的状态</param>
        public virtual void AddMaterialProcess(MaterialProcessDTO processDTO, MaterialProcessState state)
        {
            MaterialProcess process = processDTO.ToMaterialProcess();
            process.UpdateState(state);

            process.Add();

            RestoCostDomainSession.CurrentSession.SaveChanges();

            processDTO.ID = process.ID;
        }

        /// <summary>
        /// 修改一个加工单
        /// </summary>
        /// <param name="contractDTO"></param>
        /// <param name="state">加工单的状态</param>
        public virtual void UpdateMaterialProcess(MaterialProcessDTO contractDTO, MaterialProcessState state)
        {
            MaterialProcess process = MaterialProcessDQO.GetMaterialProcess(contractDTO.ID);
            process.UpdateState(state);
            
            contractDTO.CopyToMaterialProcess(process);

            process.Update();

            if (state == MaterialProcessState.Storage)
            {
                foreach (MaterialProcessItem item in process.Items)
                {
                    MaterialStorageHistory.CreateStorageHistory(item);
                }
            }

            RestoCostDomainSession.CurrentSession.SaveChanges();

        }

        public virtual void DeleteMaterialProcess(string id)
        {
            MaterialProcess supplierContract = MaterialProcessDQO.GetMaterialProcess(id);

            supplierContract.Delete();

            RestoCostDomainSession.CurrentSession.SaveChanges();

        }

        public virtual MaterialProcessDTO GetMaterialProcessDTO(string id)
        {
            MaterialProcess supplierContract = MaterialProcessDQO.GetMaterialProcess(id);

            return new MaterialProcessDTO(supplierContract);
        }

        #endregion


    }
}
